package com.doria.sso.service;

import com.doria.common.ad_enum.CodeEnum;
import com.doria.common.ad_enum.MsgEnum;
import com.doria.common.ad_enum.SMSCodeRedisKeyEnum;
import com.doria.common.pojo.vo.HttpResult;
import com.doria.sso.service.interfaces.ISmsService;
import com.doria.sso.utils.SmsUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements ISmsService {
    private final SmsUtil smsUtil;
    private final RedisTemplate<String, String> redisTemplate;

    public SmsServiceImpl(SmsUtil smsUtil, RedisTemplate<String, String> redisTemplate) {
        this.smsUtil = smsUtil;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 发送短信验证码
     * @param mobile 手机号码
     * @return 发送结果
     */
    @Override
    public HttpResult sendSmsCode(String mobile) {
        // 假装发送验证码
        try {
            String code = "123456"; // TODO 这里需要替换成随机数
            String loginCodeKey= SMSCodeRedisKeyEnum.LOGIN_CODE_KEY.getRedisKey()+mobile;
            boolean b = smsUtil.sendSms(mobile, code);
            if (b){
                // redis存一份
                redisTemplate.opsForValue().set(loginCodeKey,code);
                return new HttpResult(true, CodeEnum.SUCCESS, MsgEnum.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HttpResult(false,CodeEnum.ERROR,MsgEnum.ERROR);

    }
}
